//
//  QuickControl.m
//  FreeLimit1413
//
//  Created by mac on 14-6-25.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "QuickControl.h"
#import "CommonDefines.h"

@implementation UILabel (QuickControl)
//添加快速创建的方法
+(id)labelWithFrame:(CGRect)frame text:(NSString *)text font:(float)fontSize textColor:(UIColor *)color
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = [UIFont systemFontOfSize:fontSize];
    label.textColor = color;
    return label;
}

@end

@implementation UIButton (QuickControl)

//快速创建按钮的方法
+(id)buttonWithFrame:(CGRect)frame
               title:(NSString *)title
               image:(NSString *)image
              target:(id)target
              action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

@end

@implementation UIImageView (QuickControl)
//快速创建imageView
+(id)imageViewWithFrame:(CGRect)frame
                  image:(NSString *)image
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.image = [UIImage imageNamed:image];
    return imageView;
}

-(void)getHexagonImageViewWithLineWidth:(CGFloat)lineWidth Side:(NSInteger)sides CornerRadius:(CGFloat)cornerRadius RotationOffset:(CGFloat)rotationOffset {
    
}

-(void)getHexagonImageViewWithLineWidth:(CGFloat)lineWidth Side:(NSInteger)sides CornerRadius:(CGFloat)cornerRadius RotationOffset:(CGFloat)rotationOffset rect:(CGRect)rect{
    
    
    UIBezierPath*path = [[UIBezierPath alloc]init];
    CGFloat theta = (CGFloat)2*M_PI/(CGFloat)sides;//多边形外角和2π  这里得到的每条边转的角度。这里是60度
    //    CGFloat offset = cornerRadius * tan(theta / 2.0);
    CGFloat width = rect.size.width<rect.size.height?rect.size.height:rect.size.width;
    CGPoint center = CGPointMake( width / 2.0, width / 2.0);
    CGFloat radius = (width - lineWidth + cornerRadius - (cos(theta) * cornerRadius)) / 2.0;
    CGFloat angle = rotationOffset;
    CGPoint corner = CGPointMake(center.x + (radius - cornerRadius) * cos(angle), center.y + (radius - cornerRadius) * sin(angle));
    [path moveToPoint:CGPointMake(corner.x + cornerRadius * cos(angle + theta), corner.y + cornerRadius * sin(angle + theta))];
    
    for (int i =0; i<sides; i++) {
        angle += theta;
        CGPoint corner = CGPointMake(center.x + (radius - cornerRadius) * cos(angle), center.y + (radius - cornerRadius) * sin(angle));
        CGPoint tip = CGPointMake(center.x + radius * cos(angle), center.y + radius * sin(angle));
        CGPoint start = CGPointMake(corner.x + cornerRadius * cos(angle - theta), corner.y + cornerRadius * sin(angle - theta));
        CGPoint end = CGPointMake(corner.x + cornerRadius * cos(angle + theta), corner.y + cornerRadius * sin(angle + theta));
        [path addLineToPoint:start];
        [path addQuadCurveToPoint:end controlPoint:tip];
        
    }
    
    [path closePath];
    
    CGRect bounds = path.bounds;
    CGAffineTransform transform = CGAffineTransformMakeTranslation(-bounds.origin.x + rect.origin.x + lineWidth / 2.0, -bounds.origin.y + rect.origin.y + lineWidth / 2.0);
    [path applyTransform:transform];
    
    //
    
    CAShapeLayer *mask   = [CAShapeLayer layer];
    mask.path            = path.CGPath;
    mask.lineWidth       = lineWidth;
//    mask.strokeColor     = [UIColor clearColor].CGColor;
//    mask.fillColor       = [UIColor whiteColor].CGColor;
    self.layer.mask = mask;
    //205 162 251
    CAShapeLayer *border = [CAShapeLayer layer];
    border.path          = path.CGPath;
    border.lineWidth     = lineWidth;
    border.strokeColor   = purple_border_color.CGColor;
    border.fillColor     = [UIColor clearColor].CGColor;
    [self.layer addSublayer:border];
    
}

@end

@implementation UIView (QuickControl)
//可以为任何view设置圆角
-(void)setCornerRadius:(float)radius
{
    self.layer.cornerRadius = radius;
    self.clipsToBounds = YES;
}
-(void)setMaskWithViewWidth:(CGFloat)viewWidth withColor:(UIColor *)color {
    UIBezierPath * path = [UIBezierPath bezierPath];
    path.lineWidth = 2;
    [[UIColor whiteColor] setStroke];
    [path moveToPoint:CGPointMake((sin(M_1_PI / 180 * 60)) * (viewWidth / 2), (viewWidth / 4))];
    [path addLineToPoint:CGPointMake((viewWidth / 2), 0)];
    [path addLineToPoint:CGPointMake(viewWidth - ((sin(M_1_PI / 180 * 60)) * (viewWidth / 2)), (viewWidth / 4))];
    [path addLineToPoint:CGPointMake(viewWidth - ((sin(M_1_PI / 180 * 60)) * (viewWidth / 2)), (viewWidth / 2) + (viewWidth / 4))];
    [path addLineToPoint:CGPointMake((viewWidth / 2), viewWidth)];
    [path addLineToPoint:CGPointMake((sin(M_1_PI / 180 * 60)) * (viewWidth / 2), (viewWidth / 2) + (viewWidth / 4))];
    [path closePath];
    
    CAShapeLayer * shapLayer = [CAShapeLayer layer];
    shapLayer.lineWidth = 4*quickPlusLayoutScale;
    shapLayer.strokeColor = color.CGColor;
    shapLayer.path = path.CGPath;
    shapLayer.fillColor = [UIColor purpleColor].CGColor;
    self.layer.mask = shapLayer;
    
    CAShapeLayer *border = [CAShapeLayer layer];
    border.path          = path.CGPath;
    border.lineWidth     = 4*quickPlusLayoutScale;
    border.strokeColor   = color(205, 162, 251, 1).CGColor;
    border.fillColor     = [UIColor clearColor].CGColor;
    [self.layer addSublayer:border];
}
-(void)setMaskWithViewWidth:(CGFloat)viewWidth withColor:(UIColor *)color lineWidth:(CGFloat)width{
    UIBezierPath * path = [UIBezierPath bezierPath];
    path.lineWidth = 0;
    [[UIColor whiteColor] setStroke];
    [path moveToPoint:CGPointMake((sin(M_1_PI / 180 * 60)) * (viewWidth / 2), (viewWidth / 4))];
    [path addLineToPoint:CGPointMake((viewWidth / 2), 0)];
    [path addLineToPoint:CGPointMake(viewWidth - ((sin(M_1_PI / 180 * 60)) * (viewWidth / 2)), (viewWidth / 4))];
    [path addLineToPoint:CGPointMake(viewWidth - ((sin(M_1_PI / 180 * 60)) * (viewWidth / 2)), (viewWidth / 2) + (viewWidth / 4))];
    [path addLineToPoint:CGPointMake((viewWidth / 2), viewWidth)];
    [path addLineToPoint:CGPointMake((sin(M_1_PI / 180 * 60)) * (viewWidth / 2), (viewWidth / 2) + (viewWidth / 4))];
    [path closePath];
    
    CAShapeLayer * shapLayer = [CAShapeLayer layer];
    shapLayer.lineWidth = 1;
    shapLayer.strokeColor = color.CGColor;
    shapLayer.path = path.CGPath;
    shapLayer.fillColor = [UIColor whiteColor].CGColor;
    self.layer.mask = shapLayer;
    
    CAShapeLayer *border = [CAShapeLayer layer];
    border.path          = path.CGPath;
    border.lineWidth     = width;
    border.strokeColor   = color(205, 162, 251, 1).CGColor;
    border.fillColor     = [UIColor clearColor].CGColor;
    [self.layer addSublayer:border];
}
@end

@implementation QuickControl
//判断系统版本
+(int)osVersion
{
    //使用UIDevice设别类获取版本, 名字.....
    return [[[UIDevice currentDevice] systemVersion] intValue];
}
//获取屏幕高度
+(float)screenHeight
{
    //使用 UIScreen类获取
    return [[UIScreen mainScreen] bounds].size.height;
}
//获取屏幕宽度
+(float)screenWidth
{
    return [[UIScreen mainScreen] bounds].size.width;
}
@end
